var express=require("express");
var partials = require("express-partials");
var fs = require("fs");
var bodyParser = require("body-parser");
var cookieParser = require('cookie-parser');
var request = require('request');
var app = express();
var settings = require(__dirname + '/config.js');

var config_settings = {
	port:settings.port,
	api:settings.api[settings.environment]
};

app.use(bodyParser.urlencoded({
	extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(partials()); //layout
app.use(express.static(__dirname + "/assets"));
app.engine(".html", require("ejs").renderFile);
app.set("views", __dirname + "/views");
app.set("view engine", "html");

app.use(function(req, res, next){
	var render = res.render;
	res.render =  function(html, options, fn){
		if(!options){
			options = {}
		}

		options.config_settings =config_settings;
		render(html, options, fn);
	};
	next();
});

//controller
fs.readdirSync(__dirname + '/controller').forEach(function(name) {
	if (/Controller\.js$/.test(name)) {
		require(__dirname + '/controller/' + name)(app);
	}
});

app.listen(config_settings.port);
console.log("Server is running success on port "+config_settings.port+" enjoy it!");
